from django.db import models

class Category(models.Model):
    name = models.CharField(max_length = 255, unique = True)

    def __unicode__(self):
        return self.name
    
    
class Tool(models.Model):
    name = models.CharField(max_length = 255, unique = True)
    category = models.ForeignKey(Category)
    code = models.TextField()
    branches = models.ManyToManyField('Branch', through = 'ToolConfig')
    
    def __unicode__(self):
        return '%s, %s' % (self.category, self.name)
    

class Product(models.Model):
    name = models.CharField(max_length = 255, unique = True) 
    
    def __unicode__(self):
        return self.name


class Branch(models.Model):
    STATUS_CHOICES = ( ('ongoing', 'ongoing'), ('finished', 'finished') )
    product = models.ForeignKey(Product)
    name = models.CharField(max_length = 255)
    node_architect = models.CharField(max_length = 255)
    status = models.CharField(max_length = 255, choices = STATUS_CHOICES)
    tools = models.ManyToManyField('Tool', through = 'ToolConfig')
    components = models.ManyToManyField('Component', through = 'Target')

    def __unicode__(self):
        return "%s, %s" %(self.product, self.name)

    class Meta:
        unique_together = (("product", "name"),)


class ToolConfig(models.Model):
    branch = models.ForeignKey(Branch)
    tool = models.ForeignKey(Tool)
    weight = models.FloatField()
    config_code = models.TextField()
    rank_data = models.ManyToManyField('Component', through = 'RankData')
    
    def __unicode__(self):
        return "%s, %s" %(self.tool, self.branch)
    
    class Meta:
        unique_together = (("tool", "branch"),)


class Component(models.Model):
    product = models.ForeignKey(Product)
    name = models.CharField(max_length = 255)
    category = models.ForeignKey(Category)
    rank_data = models.ManyToManyField('ToolConfig', through = 'RankData')
    branches = models.ManyToManyField('Branch', through = 'Target')
    
    def __unicode__(self):
        return "%s, %s, %s" %(self.product, self.name, self.category)

    class Meta:
        unique_together = (("product", "category", "name"),)
        

class Target(models.Model):
    branch = models.ForeignKey(Branch)
    component = models.ForeignKey(Component)
    target_rank = models.FloatField()

    def __unicode__(self):
        return "%s, %s" %(self.branch, self.component)

    class Meta:
        unique_together = (("branch", "component"),)
            
    
class RankData(models.Model):
    component = models.ForeignKey(Component)
    tool_config = models.ForeignKey(ToolConfig)
    create_time = models.DateTimeField()
    refresh_time = models.DateTimeField()
    rank = models.FloatField()
    raw = models.TextField()
    html = models.TextField()
    
    def __unicode__(self):
        return "%s, %s, %s" %(self.tool_config, self.component, self.create_time)
    
    class Meta:
        unique_together = (("component", "tool_config", "create_time"),)
    

